package sk.fhi.server.objectify;

import java.util.ArrayList;
import java.util.List;

import sk.fhi.server.service.CRUDService;
import sk.fhi.server.util.Util;
import sk.fhi.shared.model.AbstractEntity;
import sk.fhi.shared.model.Contact;

import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.Query;

public class CRUDServiceImpl implements CRUDService {

	static {
		ObjectifyService.register(Contact.class);
	}

	@Override
	public Long save(AbstractEntity entity) {
		Objectify ofy = ObjectifyService.begin();
		return new Long(ofy.put(entity).getId());
	}

	@Override
	public void delete(Class<? extends AbstractEntity> entityClass,
			List<Long> ids) {
		Objectify ofy = ObjectifyService.begin();
		ofy.delete(Util.getKeysFromIDs(entityClass, ids));
	}

	@Override
	public <E> ArrayList<E> list(Class<E> entityClass) {
		Objectify ofy = ObjectifyService.begin();
		Query<E> query = ofy.query(entityClass);
		return (ArrayList<E>) query.list();
	}

	@Override
	public <E extends AbstractEntity> E get(Class<E> entityClass, Long id) {
		Objectify ofy = ObjectifyService.begin();
		return ofy.get(entityClass, id);
	}

}
